package com.yuntongxun.ecdemo.voicechat.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.EditText;

import com.loopj.android.http.RequestParams;
import com.yuntongxun.ecdemo.R;
import com.yuntongxun.ecdemo.common.dialog.ECProgressDialog;
import com.yuntongxun.ecdemo.voicechat.api.ApiConstants;
import com.yuntongxun.ecdemo.voicechat.bean.PostResult;
import com.yuntongxun.ecdemo.voicechat.bean.UserInfo;
import com.yuntongxun.ecdemo.voicechat.comman.Contants;
import com.yuntongxun.ecdemo.voicechat.comman.EventBusTags;
import com.yuntongxun.ecdemo.voicechat.presenter.ICommonRequestPresenter;
import com.yuntongxun.ecdemo.voicechat.presenter.impl.CommonRequestPresenterImpl;
import com.yuntongxun.ecdemo.voicechat.util.JsonHelper;
import com.yuntongxun.ecdemo.voicechat.util.PreferenceUtils;
import com.yuntongxun.ecdemo.voicechat.util.http.HttpStatusUtil;
import com.yuntongxun.ecdemo.voicechat.util.iview.ICommonViewUi;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

/**
 * 修改我会说、正在学的界面
 *
 * @author li_zhuonan
 * @version 1.0
 * @since 2016/11/23
 */

public class EditLanguagesActivity extends BaseChatActivity implements ICommonViewUi {


    @InjectView(R.id.sepak_edt)
    EditText speakEdt;
    @InjectView(R.id.learn_edt1)
    EditText learnEdt1;
    @InjectView(R.id.learn_edt2)
    EditText learnEdt2;
    @InjectView(R.id.learn_edt3)
    EditText learnEdt3;
    @InjectView(R.id.learn_edt4)
    EditText learnEdt4;

    private ICommonRequestPresenter iCommonRequestPresenter;
    private EditText[] editTexts;

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_edit_languages;
    }

    UserInfo userInfo;
    @Override
    protected void initViewsAndEvents() {
        setTitle("Languages");
        setRightTitle("Confirm");
        iCommonRequestPresenter = new CommonRequestPresenterImpl(mContext, this);
        editTexts= new EditText[]{learnEdt1, learnEdt2, learnEdt3, learnEdt4};
        String data= PreferenceUtils.getPrefString(this, Contants.Preference.user_info,"");
        if (!data.equals("")){
            JsonHelper<UserInfo> jsonHelper = new JsonHelper<UserInfo>(UserInfo.class);
            userInfo = jsonHelper.getData(data, "result");
            bindData();
        }

    }

    private void bindData(){
        speakEdt.setText(userInfo.getNativeLanguage());
        String learnLanguages=userInfo.getLearningLanguage();
        if (learnLanguages!=null&&!learnLanguages.equals("")){
            String[] languages=learnLanguages.split(",");
            for (int i=0;i<languages.length;i++){
                editTexts[i].setText(languages[i]);
            }
        }


    }

    @OnClick(R.id.toolbar_right_title)
    public void onRightClick(){
        if (TextUtils.isEmpty(speakEdt.getText().toString().trim())){
            showToastLong("Please enter your native language");
            return;
        }

        if (TextUtils.isEmpty(learnEdt1.getText().toString().trim())){
            showToastLong("Please enter your learning language");
            return;
        }

        toRequest(ApiConstants.EventTags.BEGIN_EVENT);
    }


    @Override
    public void toRequest(int eventTag) {
        if (userInfo==null){
            return;
        }
        RequestParams params = new RequestParams();
        params.put("accountName", userInfo.getAccountName());
        params.put("nativeLanguage", speakEdt.getText().toString().trim());
        StringBuilder learningLanguage=new StringBuilder();
        learningLanguage.append(learnEdt1.getText().toString().trim());
        if (!TextUtils.isEmpty(learnEdt2.getText().toString().trim())){
            learningLanguage.append(","+learnEdt2.getText().toString().trim());
        }
        if (!TextUtils.isEmpty(learnEdt3.getText().toString().trim())){
            learningLanguage.append(","+learnEdt3.getText().toString().trim());
        }
        if (!TextUtils.isEmpty(learnEdt4.getText().toString().trim())){
            learningLanguage.append(","+learnEdt4.getText().toString().trim());
        }
        params.put("learningLanguage", learningLanguage);
        iCommonRequestPresenter.request(eventTag, mContext, ApiConstants.Urls.UPDATE_INFO, params);
    }

    @Override
    public void getRequestData(int eventTag, String result) {
//        showToastLong(HttpStatusUtil.getStatusMsg(result));
        PostResult postResult = new PostResult();
        postResult.setTag(EventBusTags.UPDATE_INFO);
        EventBus.getDefault().post(postResult);
        finish();
    }

    @Override
    public void onRequestSuccessException(int eventTag, String msg) {
        showToastLong(msg);
    }

    @Override
    public void onRequestFailureException(int eventTag, String msg) {
        showToastLong(msg);
    }
    private ECProgressDialog mPostingdialog;
    @Override
    public void isRequesting(int eventTag, boolean status) {
        if(status){
            mPostingdialog = new ECProgressDialog(this, "Waiting");
            mPostingdialog.show();
        }else{
            mPostingdialog.dismiss();
        }
    }
}
